﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Kinect;

namespace KinectPowerPointControl_sample1_Fukidashi
{
    static class ProcessCatGesture
    {
        static double diffElbowSholderX = 0.15;
        static double diffElbowSholderY = 0.05;

        static bool isCatGestureActive = false;



        static Joint shoulder_right;
        static Joint shoulder_left;
        static Joint elbow_right;
        static Joint elbow_left;
        static Joint wrist_right;
        static Joint wrist_left;


        // ポーズを取得する
        public static bool pauseState(JointCollectionEquivalent joints){
           
            // スケルトン座標を取得
            if (getSkeleton(joints))
            {
                return false;
            }


            if (checkArms())
            {
                if (!isCatGestureActive)
                {
                    isCatGestureActive = true;
                    return true;
                }
            }
            else
            {
                isCatGestureActive = false;
            }

            return false;
        }

        private static bool checkArms()
        {
            // 下記の条件を判定している
            // 右肩と右手首が右ひじより上か？
            // 右肩と右手首と右ひじの横のずれがほとんどないか？
            // 左肩と左手首が左ひじより上か？
            // 左肩と左手首と左ひじの横のずれがほとんどないか？
            if (shoulder_right.Position.Y > elbow_right.Position.Y + diffElbowSholderY &&
                wrist_right.Position.Y > elbow_right.Position.Y + diffElbowSholderY &&
                System.Math.Abs(shoulder_right.Position.X - elbow_right.Position.X) < diffElbowSholderX &&
                System.Math.Abs(wrist_right.Position.X - elbow_right.Position.X) < diffElbowSholderX &&
                shoulder_left.Position.Y > elbow_left.Position.Y + diffElbowSholderY &&
                wrist_left.Position.Y > elbow_left.Position.Y + diffElbowSholderY &&
                System.Math.Abs(shoulder_left.Position.X - elbow_left.Position.X) < diffElbowSholderX &&
                System.Math.Abs(wrist_left.Position.X - elbow_left.Position.X) < diffElbowSholderX
                )
            {
                return true;
            }
            return false;
        }

        private static bool getSkeleton(JointCollectionEquivalent joints)
        {

            elbow_right = joints[JointType.ElbowRight];
            elbow_left = joints[JointType.ElbowLeft];
            shoulder_right = joints[JointType.ShoulderRight];
            shoulder_left = joints[JointType.ShoulderLeft];
            wrist_right = joints[JointType.WristRight];
            wrist_left = joints[JointType.WristLeft];

            if (elbow_left.TrackingState != JointTrackingState.Tracked ||
                elbow_right.TrackingState != JointTrackingState.Tracked ||
                shoulder_left.TrackingState != JointTrackingState.Tracked ||
                shoulder_right.TrackingState != JointTrackingState.Tracked ||
                wrist_left.TrackingState != JointTrackingState.Tracked ||
                wrist_right.TrackingState != JointTrackingState.Tracked)
            {
                //Don't have a good read on the joints so we cannot process gestures
                return true;
            }
            return false;
        }

    }
}
